//
//  ZMShareManager.swift
//  OnlineClassRoom
//
//  Created by Rowling on 2020/6/2.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import Foundation


class ZMShareManager: NSObject {

    private override init() {}
    static var shared = ZMShareManager()

    public class func isInstall(plateform : SSDKPlatformType) -> Bool{
        return ShareSDK.isClientInstalled(plateform)
    }
    
    /// 注册
    public class func shareSDKRegister() {
        ShareSDK.registPlatforms { (platformsRegister) in
            platformsRegister?.setupWeChat(withAppId: WeChatAppID, appSecret: WeChatAppSecret, universalLink: WeChatuniversalLink)
            platformsRegister?.setupQQ(withAppId: QQAppID, appkey: QQAppKey, enableUniversalLink: true, universalLink: QQuniversalLink)
//            platformsRegister?.setupAliSocial(withAppId: AliAppID)
        }
        let a = WXApi.registerApp(WeChatAppID, universalLink: WeChatuniversalLink)
        if a  {
            print("微信支付注册成功")
        }
    }
    
    /// 分享
    /// - Parameters:
    ///   - type: 0微信，1朋友圈，2QQ，3支付宝
    ///   - param: 分享参数
    public class func share(_ type: Int, _ param: NSMutableDictionary) {
        var shareType: SSDKPlatformType = .typeWechat
        switch type {
        case 0:
            shareType = .typeWechat
        case 1:
            shareType = .subTypeWechatTimeline
        case 2:
            shareType = .typeQQ
        case 3:
            shareType = .subTypeQZone
        default:
            break
        }

        ShareSDK.share(shareType, parameters: param) { (state, userData, contentEntity, error) in
            switch state {
            case .success:
                print("分享成功------")
                ZMNotification.shard.subject.onNext(.shareSuccess)
            case .fail:
                print(error!)
            case .cancel:
                print("取消分享------")
            default: break
            }
        }
    }
    
    class func share(type : SSDKPlatformType, images : [Any],title : String,content:String,shareUrl : URL , callBack:@escaping (_ state : SSDKResponseState, _ userData : [AnyHashable : Any]?,_ contentEntity : SSDKContentEntity? ,_ error:Error?)->()) {
        let shareParams = NSMutableDictionary()
        shareParams.ssdkSetupShareParams(byText: content, images: images, url: shareUrl, title: title, type: .auto)
        ShareSDK.share(type, parameters: shareParams) {(state, userData, contentEntity, error) in
            callBack(state,userData,contentEntity,error)
        }
    }
    
    class func newShare(type : SSDKPlatformType, images : [Any],title : String,content:String,shareUrl : URL , callBack:@escaping (_ state : SSDKResponseState, _ userData : [AnyHashable : Any]?,_ contentEntity : SSDKContentEntity? ,_ error:Error?)->()) {
        let shareParams = NSMutableDictionary()
        shareParams.ssdkSetupShareParams(byText: content, images: images, url: shareUrl, title: title, type: .auto)
        ShareSDK.share(type, parameters: shareParams) {(state, userData, contentEntity, error) in
            callBack(state,userData,contentEntity,error)
        }
    }
    
    /// 登录
    /// - Parameter type: 0，微信 1，QQ 2，支付宝
    public class func login(_ type: Int, _ complete: @escaping (_ user: SSDKUser?) -> Void) {
        ZMToast.showToast(title: "敬请期待")
        return;
        let loginType: SSDKPlatformType = (type == 0 ? .typeWechat : type == 1 ? .typeQQ : .typeAliSocial)
        ShareSDK.getUserInfo(loginType) { (state, user, error) in
            switch state {
            case .success:
                complete(user!)
                print(user!)
            case .fail:
                print(error!)
            case .cancel:
                print("取消操作")
            default: break
            }
        }
    }
    
    /// 授权
    /// - Parameters:
    ///   - type: type:  0 微信, 1 QQ，2 Apple
    ///   - complete: 用户信息
    public class func authorize(_ type: Int, _ complete: @escaping (_ user: SSDKUser?) -> Void) {
//        ZMToast.showToast(title: "敬请期待")
//        return;
        let loginType: SSDKPlatformType = (type == 0 ? .typeWechat : type == 1 ? .typeQQ : .typeAppleAccount)
        ShareSDK.authorize(loginType, settings: nil) { (state, user, error) in
            switch state {
            case .success:
                complete(user!)
            case .fail:
                print(error!)
            case .cancel:
                print("取消操作")
            default: break
            }
        }
    }
}
